package com.leetcode.partition1;

/**
 * @author `RKC`
 * @date 2021/10/15 8:57
 */
public class LC38外观数列 {

    public static String countAndSay(int n) {
        return recursion(n);
    }

    private static String recursion(int n) {
        if (n == 1) return "1";
        if (n == 2) return "11";
        if (n == 3) return "21";
        if (n == 4) return "1211";
        String s = recursion(n - 1);
        //对s进行描述
        StringBuilder sb = new StringBuilder();
        char c = s.charAt(0);
        int count = 1;
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == c) count++;
            else {
                sb.append(count).append(c);
                count = 1;
                c = s.charAt(i);
            }
        }
        sb.append(count).append(c);
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(countAndSay(9));
//        StringBuilder sb = new StringBuilder();
//        String s = "111221";//312211  13112221  1113213211  31131211131221
//        char c = s.charAt(0);
//        int count = 1;
//        for (int i = 1; i < s.length(); i++) {
//            if (s.charAt(i) == c) count++;
//            else {
//                sb.append(count).append(c);
//                count = 1;
//                c = s.charAt(i);
//            }
//        }
//        sb.append(count).append(c);
//        System.out.println(sb.toString());
    }
}
